package com.android.djlmapp.activity.msg;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.msg.InstationMsgAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.model.msg.InstationMsgModel;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.refresh_view.pullrecyclerview.PullRecyclerView;
import org.refresh_view.pullrecyclerview.layoutmanager.XLinearLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.android.djlmapp.activity.msg.MsgDetailActivity.INSTATION_MSG_ID_CODE;
import static com.android.djlmapp.activity.msg.MsgDetailActivity.MSG_DETAIL_TYPE_CODE;

/**
 * @Author wangziheng
 * @ProjectName djlm
 * @ClassName InstationMsgListActivity
 * @package com.android.djlmapp.activity.msg
 * @data 2018/12/20 13:44
 * @Describe 站内信消息列表
 * @Change
 */
public class InstationMsgListActivity extends BaseActivity {

    @BindView(R.id.pull_recycler_view)
    PullRecyclerView mPullRecyclerView;

    @BindView(R.id.null_data_layout_txt)
    TextView mNullDataWarningTv;
    @BindView(R.id.null_data_layout)
    RelativeLayout mNullDataLayout;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectNetErrorLayout;
    @BindView(R.id.top_bac)
    ImageView topBac;
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.tv_top_jump)
    TextView tvTopJump;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;
    @BindView(R.id.null_data_layout_img)
    ImageView nullDataLayoutImg;
    @BindView(R.id.connect_net_error_warning_tv)
    TextView connectNetErrorWarningTv;
    @BindView(R.id.requery_data_tv)
    TextView requeryDataTv;

    /**
     * 当前界面是否有数据
     */
    private boolean isHadData = false;
    private InstationMsgAdapter mAdapter;
    Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_msg_list_layout);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.GONE);
        tvName.setText("站内信");
    }

    @Override
    protected void initData() {
        queryMsgData();
        mAdapter = new InstationMsgAdapter(this);
        mPullRecyclerView.setLayoutManager(new XLinearLayoutManager(this));
        mPullRecyclerView.setColorSchemeResources(R.color.colorAccent);
        mPullRecyclerView.setAdapter(mAdapter);
        mPullRecyclerView.enableLoadMore(false);
        mPullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onPullRefresh() {
                queryMsgData();
            }

            @Override
            public void onLoadMore() {

            }
        });
        mAdapter.setOnRecyclerItemClickListener((view,position) -> {
            InstationMsgModel.DataBean dataBean = mAdapter.getItem(position);
            Intent intent = new Intent(InstationMsgListActivity.this,MsgDetailActivity.class);
            intent.putExtra(INSTATION_MSG_ID_CODE,dataBean.getMsg_id());
            intent.putExtra(MSG_DETAIL_TYPE_CODE,true);
            startActivity(intent);
        });
    }


    /**
     * 获取消息
     */
    private void queryMsgData() {

        MyApplication.getInstance().getHttpTools().getInstationMsgList(mStringCallBack);
    }

    private StringCallback mStringCallBack = new StringCallback() {
        @Override
        public void onSuccess(Response<String> response) {
            try {
                String resultStr = response.body();
                Gson gson = new Gson();
                InstationMsgModel resultData = gson.fromJson(resultStr, InstationMsgModel.class);
                if (resultData != null) {
                    if (resultData.getCode() == 200) {
                        List<InstationMsgModel.DataBean> msgModels = resultData.getData();
                        if (msgModels != null && msgModels.size() > 0) {
                            normalLayout();
                            isHadData = true;
                            mAdapter.clear();
                            mAdapter.addAll(msgModels);
                        } else if (!isHadData) {
                            noDataLayout();
                        }
                    } else {
                        noDataLayout();
                        showToast(resultData.getMsg());
                    }
                } else {
                    noDataLayout();
                    showToast("服务端数据出错，请刷新重试");
                }
            } catch (Exception ex) {

            }
        }

        @Override
        public void onError(Response<String> response) {
            if (!InstationMsgListActivity.this.isFinishing() || !InstationMsgListActivity.this.isDestroyed()) {
                if (isHadData) {
                    showToast(getString(R.string.connect_sever_error_msg));
                } else {
                    connectNetErrorLayout();
                }
            }
        }

        @Override
        public void onFinish() {
            if (mPullRecyclerView != null) {
                mPullRecyclerView.stopRefresh();
            }
        }
    };

    /**
     * 无数据状态
     */
    private void noDataLayout() {
        if (mNullDataLayout.getVisibility() == View.GONE) {
            mConnectNetErrorLayout.setVisibility(View.GONE);
            mNullDataLayout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 网络错误状态
     */
    private void connectNetErrorLayout() {
        if (mConnectNetErrorLayout.getVisibility() == View.GONE) {
            mNullDataLayout.setVisibility(View.GONE);
            mConnectNetErrorLayout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 正常布局状态
     */
    private void normalLayout() {
        if (mConnectNetErrorLayout.getVisibility() == View.VISIBLE || mNullDataLayout.getVisibility() == View.VISIBLE) {
            mConnectNetErrorLayout.setVisibility(View.GONE);
            mNullDataLayout.setVisibility(View.GONE);
        }
    }
}
